<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "set_user_up".
 *
 * @property int $id
 * @property float|null $vip1
 * @property float|null $vip2
 * @property float|null $vip3
 * @property float|null $vip1_1
 * @property float|null $vip1_2
 * @property float|null $vip1_3
 * @property float|null $vip2_1
 * @property float|null $vip2_2
 * @property float|null $vip2_3
 * @property float|null $vip3_1
 * @property float|null $vip3_2
 * @property float|null $vip3_3
 * @property int|null $profit_num 分红前多少人
 * @property int|null $profit_ratio 分红比例
 * @property int|null $help_ratio 帮扶比例
 */
class SetUserUp extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_user_up';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['vip1', 'vip2', 'vip3', 'vip1_1', 'vip1_2', 'vip1_3', 'vip2_1', 'vip2_2', 'vip2_3', 'vip3_1', 'vip3_2', 'vip3_3'], 'number'],
            [['profit_num', 'profit_ratio', 'help_ratio'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'vip1' => 'Vip 1',
            'vip2' => 'Vip 2',
            'vip3' => 'Vip 3',
            'vip1_1' => 'Vip 1  1',
            'vip1_2' => 'Vip 1  2',
            'vip1_3' => 'Vip 1  3',
            'vip2_1' => 'Vip 2  1',
            'vip2_2' => 'Vip 2  2',
            'vip2_3' => 'Vip 2  3',
            'vip3_1' => 'Vip 3  1',
            'vip3_2' => 'Vip 3  2',
            'vip3_3' => 'Vip 3  3',
            'profit_num' => 'Profit Num',
            'profit_ratio' => 'Profit Ratio',
            'help_ratio' => 'Help Ratio',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetUserUp' => $data]) && $model->save()) {
            return true;
        }
    }
}
